// 类的类型兼容性
class A {
  x: number = 1;
  y: number = 2;
}
class B {
  x: number = 3;
  y: number = 4;
  z: number = 5;
}
let a: A = new A();
console.log(a);

// 类型兼容性, 属性多的赋值给属性少的对象不会报错, 反之会报错
let a2: A = new B();
console.log(a2);
// let b2: B = new A();//报错
// console.log(b2);

// 接口的类型兼容性
interface IA {
  x: number;
  y: number;
  z: number;
}

interface IB {
  x: number;
  y: number;
}

let ia: IA = { x: 1, y: 2, z: 3 };
let ib: IB = ia;
console.log(ib);

let ib2: IB = { x: 1, y: 2 };
// let ia2: IA = ib2; //会报错

//函数的类型兼容性
interface Point2D {
  x: number;
  y: number;
}
interface Point3D {
  x: number;
  y: number;
  z: number;
}

type F1 = (a: Point2D) => string; //声明,两个参数
type F2 = (a: Point3D) => string; //三个

let fn1: F1 = (a = { x: 1, y: 2 }) => {
  return "admin1";
};
let fn2: F2 = (a = { x: 3, y: 4, z: 5 }) => {
  return "admin2";
};

// 参数的数量: //参数少的可以赋值给参数多的
// fn1 = fn2;
fn2 = fn1;
let v = fn1({ x: 66, y: 88 });
console.log(fn2);
console.log(v);

// 返回值类型
type F3 = () => string;
type F4 = () => string;

let f3: F3 = () => {
  return "admin3";
};
let f4: F4 = () => {
  return "admin4";
};

// f3 = f4;
// console.log(f3());
f4 = f3;
console.log(f4());

type F5 = () => { name: string };
type F6 = () => { name: string; age: number };

let f5: F5 = () => {
  return { name: "admin5" };
};
let f6: F6 = () => {
  return { name: "admin6", age: 22 };
};

f5 = f6; //返回值多的可以赋值给返回值少的函数, 反之不能赋值
console.log(f5());
// f6 = f5;
// console.log(f6());
